<?php

session_start();

require_once($_SERVER['DOCUMENT_ROOT'].'/incofarming/Functions/config.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/incofarming/Functions/class/admin_model.php');

$reg_kop = new Register_koperasi();

if(isset($_POST['tambah-kop'])){
	$reg_kop->insert_koperasi();
} else {
	$reg_kop->form_add_koperasi();
}

class Register_koperasi{
	
	private $admin_model;
	
	function __construct(){
		$this->admin_model = new Admin_model();
	}
	
	function form_add_koperasi(){
		
		$prov_res = $this->admin_model->get_all_prov();
		$prov_arr = array();
		while($row = mysql_fetch_array($prov_res)){
			$temp_prov = array(
				'id_prov' => $row['IDPROPINSI'],
				'nama_prov' => $row['NAMAPROPINSI']
			);
			
			$kota_res = $this->admin_model->get_kota_from_idprov($row['IDPROPINSI']);
			$kota_arr = array();
			while($row2 = mysql_fetch_array($kota_res)){
				$temp_kota = array(
					'id_kota' => $row2['IDKOTA'],
					'nama_kota' => $row2['NAMAKOTA']
				);
				
				$kota_arr[] = $temp_kota;
			}
			
			$kombin_arr = array(
				'data_prov' => $temp_prov,
				'data_kota' => $kota_arr
			);
			
			$prov_arr[] = $kombin_arr;
			
		}
		
		include($_SERVER['DOCUMENT_ROOT'].'/incofarming/Views/admin/register-koperasi_view.php');
	}
	
	function insert_koperasi(){
		$kop_arr = array(
			'NAMAKOPERASI' => "'".$_POST['nama-koperasi']."'",
			'NOTELP' => "'".$_POST['no-telp']."'",
			'NOFAX' => "'".$_POST['no-fax']."'",
			'TGLBERDIRI' => "'".$_POST['tgl-berdiri']."'",
			'INFORMASI' => "'".$_POST['info-koperasi']."'",
			'ALAMAT' => "'".$_POST['alamat-koperasi']."'",
			'EMAIL' => "'".$_POST['email']."'",
			'IDKOTA' => $_POST['id-kota']
		);
		
		$user_arr = array(
			'USERNAME' => "'".$_POST['username']."'",
			'PASSWORD' => "'".md5($_POST['password'])."'",
			'IDPRIVILEGE' => 1
		);//var_dump($user_arr);
		
		$this->admin_model->insert_user($user_arr);
		$user_res = $this->admin_model->get_user_id();
			$id_user = 0;
		while($row = mysql_fetch_array($user_res)){
			$id_user = $row['IDUSER'];
		}
			
		$kop_arr['IDUSER'] = $id_user;
		
		
		if ($_FILES["logo-koperasi"]["error"] <= 0){
			
			echo "Upload: " . $_FILES["logo-koperasi"]["name"] . "<br>";
			echo "Type: " . $_FILES["logo-koperasi"]["type"] . "<br>";
			echo "Size: " . ($_FILES["logo-koperasi"]["size"] / 1024) . " kB<br>";
			echo "Stored in: " . $_FILES["logo-koperasi"]["tmp_name"];
			
			
			if (file_exists("upload/" . $_FILES["logo-koperasi"]["name"])){
				echo $_FILES["logo-koperasi"]["name"] . " already exists. ";
			} else {
				$nama_foto = $_FILES["logo-koperasi"]["name"];
				$loc = $_SERVER['DOCUMENT_ROOT'].'/incofarming/Views/img/koperasi/'.$nama_foto;
				move_uploaded_file($_FILES["logo-koperasi"]["tmp_name"], $loc);
				
				$photo_arr = array(
					'NAMAFOTO' => "'".$nama_foto."'",
					'DIREKTORI' => "'".$loc."'"
				);
				
				$this->admin_model->insert_photo($photo_arr);
				
				$foto_res = $this->admin_model->get_foto_id();
				$id_foto = 0;
				while($row = mysql_fetch_array($foto_res)){
					$id_foto = $row['IDFOTO'];
				}
				
				$kop_arr['IDFOTO'] = $id_foto;
							
			}
			
		}
		//var_dump($kop_arr);
		$this->admin_model->insert_kop($kop_arr);
		
		header('Location: /incofarming/Functions/admin/dashboard-admin.php');
				
	}
	
}

?>